/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package GUIUtilities;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Point;
import javax.swing.JLabel;

/**
 *
 * @author Heather
 */
public abstract class LabelManager {

  public static void centerLabelAtPoint(JLabel l, Point p)
  {
    Dimension dim = l.getSize();
    int xShift = (int)(dim.getWidth()/2.0);
    int yShift = (int)(dim.getHeight()/2.0);

    int xCorner = p.x - xShift;
    int yCorner = p.y - yShift;

    Point corner = new Point(xCorner, yCorner);
    l.setLocation(corner);
  }

  // resizes a label based on its font
  public static void resizeLabel(JLabel l)
  {
    Dimension lD = getLabelDimensions(l);
    l.setSize(lD);
  }

  public static Dimension getLabelDimensions(JLabel l)
  {
    Font f = l.getFont();
    FontMetrics fm = l.getFontMetrics(f);

    String labelText = l.getText();
    // first we need to check for html tags...
    boolean htmlExists = false;
    if (labelText.length() >= 13)
    {
      // check if there's an html starter tag
      String starterTag = labelText.substring(0, 6);

      // check if there's a html end tag
      String endTag = labelText.substring(labelText.length() - 7, labelText.length());

      if (starterTag.equals("<html>") && endTag.equals("</html>"))
      {
        htmlExists = true;
      }

      // if there's html, remove the tags
      if (htmlExists)
      {
        String noTags = labelText.substring(6, labelText.length() - 7);
        labelText = noTags;
      }
    }

    if (htmlExists)
    {
      // calculate the number of lines required for display purposes
      String maxSubString = "";
      int numberOfLines = 0;
      char[] labelArray = labelText.toCharArray();

      // stores what we think is a substring representing a break
      String breakTracker = "";
      // this is the largest substring read that DOES NOT contain a break
      String subStringBeforeBreak = "";
      for (int i = 0; i < labelArray.length; i++)
      {
        char c = labelArray[i];
        // check for first breaker symbol
        if (c == '<')
        {
          // regardless of what the break tracker has seen, this character indicates
          // the beginning of a new tracker.
          subStringBeforeBreak += breakTracker;
          breakTracker = "<";
        }
        else
        {
          if (c == 'p')
          {
            if (breakTracker.equals("<"))
            {
              breakTracker += c;
            }
            else // else, we just add what we had of the breaktracker to the substring
            {
              subStringBeforeBreak += breakTracker;
              subStringBeforeBreak += c;
              breakTracker = "";
            }
          }
          else
          {
            if (c == '>')
            {
              // we know we have a full break if we get into this if statement
              if (breakTracker.equals("<p"))
              {
                // full break means we need to increment the linecounter
                numberOfLines++;
                // now we need to see if our substring is bigger than the max substring
                if (subStringBeforeBreak.length() > maxSubString.length())
                {
                  maxSubString = subStringBeforeBreak;
                  subStringBeforeBreak = "";
                }

                breakTracker = "";
              }
              else // else, we just add what we had of the breaktracker to the substring
              {
                subStringBeforeBreak += breakTracker;
                subStringBeforeBreak += c;
                breakTracker = "";
              }
            }
            // if we've reached here, then we automatically know that there's no break
            // moreover, we know that everything we've tracked as a possible break is
            // NOT a break, so we can safely add it to the substringbeforebreak
            else
            {
              subStringBeforeBreak += breakTracker;
              subStringBeforeBreak += c;
              breakTracker = "";
            }
          }
        }
      }

      // now we need to see if our substring is bigger than the max substring
      if (subStringBeforeBreak.length() > maxSubString.length())
      {
        maxSubString = subStringBeforeBreak;
        subStringBeforeBreak = "";
      }

      if (subStringBeforeBreak.length() > 0)
      {
        numberOfLines++;
      }

      return new Dimension(fm.stringWidth(maxSubString), fm.getHeight() * numberOfLines);
    }
    else // if no html, just use the label text as is
    {
      return new Dimension(fm.stringWidth(l.getText()), fm.getHeight());
    }
  }
}
